package the_Crash;

import java.util.ArrayList;

import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;

public class Accident extends BaseSimEntity {
	
	private static long totalAccidents=0;
	private int cleanUpTime;
	private int counter = 0;
	private NdPoint continuousLocation;

	public Accident(NdPoint continuousLocation, GridPoint location, Grid<Object> grid,
			ContinuousSpace<Object> space) {
		super(location, grid, space);
		this.continuousLocation = continuousLocation;
		cleanUpTime = RandomHelper.nextIntFromTo(ParameterResolver.getMinCleanUpTime(), ParameterResolver.getMaxCleanUpTime());
		totalAccidents++;
	}
	
	public static void globalReset() {
		totalAccidents=0;
	}
	
	public void initialize()
	{
		space.moveTo(this, continuousLocation.getX(), continuousLocation.getX());
	}
	
	//check for accident cleanup
	@ScheduledMethod (start =1, interval = 1)
	public void step(){
		counter++;
		if(counter == cleanUpTime){
			CityBuilder.removeFromContext(this);
		}
	}
	
	//Output for a dataset
	//returns the mean accidents per tick calculated over the lifetime of the simulation
	public static double getAccidentsPerCar()
	{
		return (double)100*totalAccidents/Car.getTotalCars();
	}
}
